﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Configuration;

namespace Common
{
    public class ReportConfig
    {
       public List<ReportConfigItem> rpcItems { get { return ParseTestItems("Report.xml");} }
       public static string CurrentModel { get { return ConfigurationManager.AppSettings["CurrentModel"]; } }
       public bool isConfigFileExist()
       {
           return System.IO.File.Exists("Report.xml");
       }

       public bool isItemExist(string ItemName)
       {
           bool exist = false;
           foreach(var item in  rpcItems)
           {
               if (item.Name == ItemName)
               {
                   exist = true;
                   break;
               }
           }
           return exist;
       }
       public bool isModelExist(string ModelName)
       {
           bool exist = false;
           List<string> models = getReportModels();
           foreach (var item in models)
           {
               if (item == ModelName)
               {
                   exist = true;
                   break;
               }
           }
           return exist;
       }
       public ReportConfigItem getReportItembyName(string ItemName)
       {
           foreach (var item in rpcItems)
           {
               if (item.Name == ItemName)
               {
                   return item;
               }
           }
           return null;
       }
       public List<string> getReportModels()
       {
           List<string> Models = new List<string>();
           List<ReportConfigItem> ReportConfigItems = new List<ReportConfigItem>();
           XmlNodeList nodes = XMLHelper.GetXmlNodeListByXpath("Report.xml", "Root");
           foreach (XmlNode node in nodes)
           {
               foreach (XmlNode xn in node.ChildNodes)
               {
                   Models.Add(xn.Name);
               }
           }
           return Models;
       }

       private static List<ReportConfigItem> ParseTestItems(string FileName)
       {
           List<ReportConfigItem> ReportConfigItems = new List<ReportConfigItem>();
           XmlNodeList nodes = XMLHelper.GetXmlNodeListByXpath(FileName, string.Format(@"Root/{0}", CurrentModel));
           foreach (XmlNode item in nodes)
           {
               foreach (XmlNode xn in item.ChildNodes)
               {
                   ReportConfigItem ri = new ReportConfigItem();
                   ri.Name = xn.Name;
                   foreach (XmlNode xn1 in xn.ChildNodes)
                   {
                       if (xn1.Name == "USL")
                       {
                           string strUSL = xn1.InnerText;
                           ri.USL = double.Parse(strUSL);
                       }
                       else if (xn1.Name == "LSL")
                       {
                           string strLSL = xn1.InnerText;
                           ri.LSL = double.Parse(strLSL);
                       }
                       else if (xn1.Name == "UseMarkVal")
                       {
                           string strUseMarkVal = xn1.InnerText;
                           ri.UseMarkVal = int.Parse(xn1.InnerText.Trim('"')) > 0 ? true : false;

                       }
                       else if (xn1.Name == "MarkVal")
                       {
                           string strMarkVal = xn1.InnerText;
                           if (!string.IsNullOrEmpty(strMarkVal))
                           {
                               string[] dbs = strMarkVal.Split(new char[] { ',' });
                               List<double> listdbs = new List<double>();
                               foreach (var db in dbs)
                               {
                                   listdbs.Add(double.Parse(db));
                               }
                               ri.MarkVals = listdbs.ToArray();
                           }
                       }

                   }
                   ReportConfigItems.Add(ri);
               }
           }
           return ReportConfigItems;
       }
    }

}
